const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    openId: '',
    coach: {},
    coachLogin: false,
    registration: false //是否注册
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  coachLogin: function() {
    wx.navigateTo({
      url: '/pages/registration/registration'
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    const that = this;
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: res => {
              app.globalData.userInfo = res.userInfo;
              that.setData({
                userInfo: res.userInfo
              })
            }
          })
        }
      }
    });
    app.userCheckCallback = res => {
      this.setData({
        openId: res
      });
      this.getInfo();
    };
    const openId = app.globalData.openId;
    const coachLogin = app.globalData.coachLogin || false;
    this.setData({
      openId,
      coachLogin
    });
  },
  onPullDownRefresh: function () {
    this.getInfo();
  },
  getInfo: function() {
    const that = this;
    const openId = that.data.openId || app.globalData.openId;
    if (openId.length==0){
      return;
    }
    const url = app.globalData.url;
    wx.showNavigationBarLoading();
    wx.request({
      url: url + '/coach/' + openId,
      success: function(response) {
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
        if (response.data.success) {
          const content = response.data.content;
          if (content.avatarUrl) {
            content.avatarUrl += '?number=' + Math.random();
          }
          that.setData({
            coach: content,
            registration: true
          });
        } else {
          wx.showToast({
            title: response.data.message,
            icon: 'none'
          })
        }
      }
    })
  },
  changeName: function() {
    const name = this.data.coach.name;
    wx.setStorageSync("name", name);
    wx.navigateTo({
      url: '/pages/nameReset/nameReset',
    })
  },
  changeAvatar: function() {
    const avatarUrl = this.data.coach.avatarUrl;
    wx.setStorageSync("avatarUrl", avatarUrl);
    wx.navigateTo({
      url: '/pages/avatarChange/avatarChange',
    })
  },
  changePicture: function() {
    const fitnessPicSrc = this.data.coach.fitnessPicSrc;
    wx.setStorageSync("fitnessPicSrc", fitnessPicSrc);
    wx.navigateTo({
      url: '/pages/coachPicture/coachPicture',
    })
  },
  video: function() {
    wx.navigateTo({
      url: '/pages/video/video',
    })
  },
  toAbility: function() {
    const skills = this.data.coach.skills;
    wx.setStorageSync("skills", JSON.stringify(skills));
    wx.navigateTo({
      url: '/pages/ability/ability',
    })
  },
  toAboutMe: function() {
    const about = this.data.coach.about;
    wx.setStorageSync("about", about);
    wx.navigateTo({
      url: '/pages/about/about',
    })
  },
  toCertificates: function() {
    const certificates = this.data.coach.certificates;
    wx.setStorageSync("certificates", JSON.stringify(certificates));
    wx.navigateTo({
      url: '/pages/certificates/certificates',
    })
  },
  importFace:function(){
    
  }
})